package com.antdesignbackend.backend.register.controller;

import com.antdesignbackend.backend.animalNumber.entity.AnimalNumber;
import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.common.CalendarDate;
import com.antdesignbackend.backend.entity.Enterprise;
import com.antdesignbackend.backend.entity.Users;
import com.antdesignbackend.backend.service.EnterpriseService;
import com.antdesignbackend.backend.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 企业注册
 *
 * @author tang,qm
 * @email tang,qm@163.com
 * @date 2020-07-22 11:43:40
 */

@Controller
@RequestMapping("/enterprise")
@Slf4j
public class EnterpriseController extends BaseController{

	private static final String FUN_NAME = "企业注册";//功能名称

	private int day1 = 48; //哺乳转保育时间
	private int day2 = 60; //保育转育成时间

	@Autowired
	private EnterpriseService enterpriseService;

	@Autowired
	UserService userService;
	@Autowired
	AnimalNumberService animalNumberService;

	@GetMapping("/register")
	public String index() {
		return "register/enterprise/index";
	}



	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Enterprise enterprise){
		ReturnVO result = new ReturnVO();
		enterprise.setCreateTime(new Date());
		enterprise.setExamine(0);
		enterprise.setActivity(1);
		//加密初始密码
		enterprise.setPassword(DigestUtils.md5DigestAsHex(enterprise.getPassword().getBytes()));
		QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("EnterpriseName",enterprise.getEnterpriseName()).or().eq("IDCard",enterprise.getIdCard());
		if(enterpriseService.list(queryWrapper).isEmpty() == false){
			result.setMessage("该企业已存在！");
			result.setCode("500");
			return result;
		}
		queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("LoginID",enterprise.getLoginID());
		if(enterpriseService.list(queryWrapper).isEmpty() == false){
			result.setMessage("该登录账号已存在！");
			result.setCode("500");
			return result;
		}
		QueryWrapper<Users> userWrapper = new QueryWrapper<>();
		userWrapper.eq("UserName",enterprise.getLoginID());
		if(userService.list(userWrapper).isEmpty() == false){
			result.setMessage("该登录账号已存在！");
			result.setCode("500");
			return result;
		}
		try {
			if (enterprise.getTypes() == 1){
				enterprise.setDay1(day1);
				enterprise.setDay2(day2);
			}
			if(enterpriseService.save(enterprise)){
				if (enterprise.getTypes() == 1){
					//创建一条初始化记录
					AnimalNumber animalNumber = new AnimalNumber();
					animalNumber.setEid(enterprise.getEnterpriseId());
					animalNumber.setCreateTime(new Date());
					animalNumber.setAnimalType(enterprise.getAnimalTypes());
					animalNumber.setChannel(1);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					animalNumber.setBatchNumber(sdf.format(new Date()));
					if (enterprise.getAnimalTypes()== 1){
						animalNumber.setSpecies(enterprise.getSpecies());
						animalNumber.setReserve(enterprise.getReserve());
						animalNumber.setMenstruation(enterprise.getMenstruation());
						animalNumber.setLactation(enterprise.getLactation());
						animalNumber.setConservation(enterprise.getConservation());
						animalNumber.setCommodity(enterprise.getCommodity());
						animalNumber.setStarTime(new Date());
						animalNumber.setConservationTime(CalendarDate.addDate(new Date(),"day",day1));
						animalNumber.setGrowTime(CalendarDate.addDate(new Date(),"day",day1+day2));
					}else {
						animalNumber.setLivestock(enterprise.getLivestock());
					}
					animalNumberService.save(animalNumber);
				}
				result.setMessage("保存成功！");
				result.setCode("200");
			}else{
				result.setMessage("保存失败！");
				result.setCode("500");
			}
		}catch (Exception e){
			log.error(e.getMessage());
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

}
